<template>
  <a-row :gutter="24">
    <a-col :md="24">
      <a-card :style="cardStyle" :bordered="false">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
          <!-- 搜索区域 -->
          <a-form layout="inline" @keyup.enter.native="searchQuery">
            <a-row :gutter="24">
              <a-col :md="6" :sm="24">
                <a-form-item label="任务类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-input placeholder="请输入任务类型（销售，采购，两个字即可）" v-model="queryParam.taskType"></a-input>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="处理状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-select default-value="lucy" v-model="queryParam.status" placeholder="选择处理状态">
                    <a-select-option value="未处理">未处理</a-select-option>
                    <a-select-option value="通过">通过</a-select-option>
                    <a-select-option value="拒绝">拒绝</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="发起人" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-select placeholder="请选发起人" showSearch optionFilterProp="children" v-model="queryParam.creator">
                    <a-select-option v-for="(item,index) in userList" :key="index" :value="item.id">
                      {{ item.userName }}
                    </a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                <a-col :md="6" :sm="24">
                  <a-button type="primary" @click="searchQuery">查询</a-button>
                  <a-button style="margin-left: 8px" @click="searchReset">重置</a-button>
                   <a @click="handleToggleSearch" style="margin-left: 8px">
                    {{ toggleSearchStatus ? '收起' : '展开' }}
                    <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
                  </a>
                </a-col>
              </span>
              <template v-if="toggleSearchStatus">

              </template>
            </a-row>
          </a-form>
        </div>
        <div>
          <a-table
            ref="table" size="middle" bordered rowKey="id"
            :columns="defColumns" :dataSource="dataSource" :components="handleDrag(columns)"
            :pagination="ipagination" :scroll="scroll" :loading="loading" @change="handleTableChange"
          >
            <!--操作-->
            <span slot="action" slot-scope="text, record">
              <template v-if="record.status=='未处理'">
                <template v-if="record.taskType.slice(-2) == '审核' ">
                  <a @click="thisDispose(record)">任务处理</a>
                </template>

                <template v-if="record.taskType.slice(-2) == '任务' ">
                  <a-popconfirm
                    title="是否已完成当前任务？"
                    ok-text="是"
                    cancel-text="否"
                    @confirm="taskCompleted(record)"
                  >
                    <a>处理完成</a>
                  </a-popconfirm>
                </template>
<!--                <a-divider  v-if="record.taskType.slice(-2)=='审核'" type="vertical"/>-->
<!--                <a @click="pageDispose(record)" v-if="record.taskType.slice(-2)=='审核'">页面跳转</a>-->
              </template>
            </span>
            <!--单据-->
            <span slot="taskCode" slot-scope="text, record">
              <a @click="myHandleDetail(record)">{{record.relatedId}}</a>
            </span>
            <!--优先级-->
            <span slot="priority" slot-scope="text, record">
              <a-tag v-if="text ===1" color="red">紧急</a-tag>
              <a-tag v-if="text ===2" color="orange">普通</a-tag>
              <a-tag v-if="text ===3" color="blue">延后</a-tag>
            </span>
            <!--状态-->
            <span slot="status" slot-scope="text, record">
              <a-tag v-if="text == '未处理'" color="orange">{{text}}</a-tag>
              <a-tag v-if="text == '已处理'" color="green">{{text}}</a-tag>
              <a-tag v-if="text == '通过'" color="green">{{text}}</a-tag>
              <a-tag v-if="text == '拒绝'" color="red">{{text}}</a-tag>
            </span>
          </a-table>
        </div>
        <!--Model1-->
        <a-modal v-model="auditModal.visible" title="任务弹窗" @ok="auditModalOk" width="40%" :keyboard="false" :maskClosable="false" @cancel="closeModal">
          <a-form style="margin-right: 50px">
            <a-form-item label="审批意见:" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-radio-group name="radioGroup" v-model="auditModal.auditResult">
                <a-radio :value="1">同意</a-radio>
                <a-radio :value="0">拒绝</a-radio>
              </a-radio-group>
            </a-form-item>
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="附件" data-step="10" data-title="附件" data-intro="可以上传与单据相关的图片、文档，支持多个文件">
                <j-upload v-model="auditModal.fileList" bizPath="bill"></j-upload>
              </a-form-item>
            <a-form-item label="审批留言:" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea placeholder="审批留言~" :rows="4" v-model="auditModal.statusRemark"/>
            </a-form-item>
          </a-form>
        </a-modal>
        <bill-detail ref="modalDetail" @ok="modalFormOk" @close="modalFormClose"></bill-detail>
      </a-card>
    </a-col>
  </a-row>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import {getUserList} from '@/api/api'
import JUpload from '@/components/jeecg/JUpload'
import { deleteAction, getAction, postAction, downFile, downFilePost, getFileAccessHttpUrl } from '@/api/manage'
import BillDetail from '../bill/dialog/BillDetail.vue'
export default {
  name: "BackLog",
  components: { BillDetail,JUpload},
  mixins:[JeecgListMixin],
  data () {
    return {
      //用户数据
      userList:[],
      //搜索-展开收起
      toggleSearchStatus:false,
      //审核框数据
      auditModal:{
        visible:false,
        auditResult:1,
        fileList:'',
        statusRemark:undefined,
        id:undefined,
      },
      // 查询条件
      queryParam: {
        taskType:undefined,
        status:undefined,
        creator:undefined,
      },
      labelCol: {
        span: 5
      },
      wrapperCol: {
        span: 18,
        offset: 1
      },
      // 默认索引
      defDataIndex:['taskType','relatedId','creatorId','createTime'],
      // 默认列
      defColumns: [
        {
          title:'#',
          dataIndex: 'rowIndex', width:40, align:"center", slots: { title: 'customTitle' },
          customRender:function (t,r,index) {
            return (t !== '合计') ? (parseInt(index) + 1) : t
          }
        },
        { title: '操作', dataIndex: 'system',width:180,align: 'center',  scopedSlots: { customRender: 'action'},},
        { title: '单据编号', dataIndex: 'relatedId',width:150,scopedSlots: {customRender:'taskCode'},},
        { title: '任务类型', dataIndex: 'taskType',width:110},
        { title: '优先级', dataIndex: 'priority',width:70,scopedSlots: { customRender: 'priority'}},
        { title: '处理状态', dataIndex: 'status',width:80, scopedSlots: { customRender: 'status'}},
        { title: '发起人', dataIndex: 'creatorName',width:180},
        { title: '处理人', dataIndex: 'assigneeName',width:180},
        { title: '转交人', dataIndex: 'turnName',width:180},
        {
          title: '创建时间',
          dataIndex: 'createTime',
          width: 145,
          customRender: (text) => {
            // 格式化时间
            const date = new Date(text);
            const year = date.getFullYear();
            const month = String(date.getMonth() + 1).padStart(2, '0');
            const day = String(date.getDate()).padStart(2, '0');
            const hours = String(date.getHours()).padStart(2, '0');
            const minutes = String(date.getMinutes()).padStart(2, '0');
            const seconds = String(date.getSeconds()).padStart(2, '0');
            return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
          }
        },
        { title: '处理说明', dataIndex: 'statusRemark',width:300},
      ],
      url: {
        list: "/todoTask/list",
        update:"/todoTask/update",
        delete: "/depotHead/delete",
        deleteBatch: "/depotHead/deleteBatch",
        batchSetStatusUrl: "/todoTask/taskAudit",
        taskFinished:"/todoTask/taskFinished",
      }
    }
  },
  created () {
  },
  methods:{
    //task 任务完成触发事件
    taskCompleted(record){
      postAction(this.url.taskFinished+"?id="+record.id,null).then((res)=>{
        if(res.success){
          this.$message.success('成功');
        } else {
          this.$message.warning('失败');
        }
        this.loadData();
      })
    },
    //关闭modal
    closeModal(){
      this.auditModal ={
        visible:false,
        auditResult:1,
        fileList:undefined,
        statusRemark:undefined,
        id:undefined,
      };
    },
    //加载user
    initUser() {
      getUserList({}).then((res)=>{
        if(res) {
          this.userList = res;
        }
      });
    },
    //审批弹窗点击确认
    auditModalOk(){
      postAction(this.url.batchSetStatusUrl,this.auditModal).then((res) => {
        if(res.success){this.$message.success('操作成功');} else {this.$message.warning('操作失败');}
        this.loadData();
        this.auditModal.visible = false;
      })
    },
    //任务处理
    thisDispose(record){
      if(record.taskType.slice(-2)==='任务'){
        const temp = record.taskType.slice(0,-2)
        //修改状态
        const form = {};
        form.id = record.id;
        postAction(this.url.update, form).then((res) => {
          if(res.code === 200){
            this.$message.success('成功');
          } else {
            this.$message.warning('失败');
          }
          this.loadData();
        })
        //跳转
        if(temp === '销售订单'){
          this.$router.push('/bill/purchase_apply')
        }else if(temp==='请购单'){
          this.$router.push('/bill/purchase_order')
        }else if(temp==='采购订单'){
          this.$router.push('/bill/purchase_in')
        }else if(temp==='采购入库'){
          this.$router.push('/bill/purchase_in')
        }
      }else{
        this.auditModal.id = record.id;
        this.auditModal.visible = true;
      }
    },
    pageDispose(record){
      const temp = record.taskType.slice(0,-2)
      if(temp === '销售订单'){
        this.$router.push('/bill/sale_order')
      }else if(temp === '销售出库'){
        this.$router.push('/bill/sale_out')
      }else if(temp === '请购单'){
        this.$router.push('/bill/purchase_apply')
      }else if(temp === '采购订单'){
        this.$router.push('/bill/purchase_order')
      }else if(temp === '采购入库'){
        this.$router.push('/bill/purchase_in')
      }
    },
    //查看详情
    myHandleDetail(record, type, prefixNo) {
      if(this.btnEnableList.indexOf(7)===-1) {
        this.$refs.modalDetail.isCanBackCheck = false
      }
      record.number = record.relatedId;
      this.handleDetail(record,record.taskType.slice(0,-2));
    },
  },
  mounted() {
    this.initUser();
  },
}
</script>